<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备名称" prop="name">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="接入方式" prop="access">
          <el-select v-model="queryParams.access" placeholder="请选择接入方式" clearable style="width: 150px;">
            <el-option
                v-for="dict in iot_access"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top: 20px;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['iot:product:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['iot:product:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['iot:product:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['iot:product:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-empty description="暂无设备" v-if="productList.length <= 0"></el-empty>

      <el-row v-else>
        <el-col v-for="(item, index) in productList" :key="index"
                :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <div class="card">
            <div class="card-content">
              <div class="card-content-img">
                <img class="productImg" src="@/assets/images/shebei.png">
              </div>
              <div class="card-content-right">
                <div class="card-content-right-title">
                  <span>{{ item.name }}</span>
                </div>
                <div class="card-content-right-info">
                  <div class="card-content-right-info-one">
                    <div class="card-content-right-info-one-topic">设备类型</div>
                    <div class="card-content-right-info-one-content">{{ item.categoryName}}</div>
                  </div>
                  <div class="card-content-right-info-two">
                    <div class="card-content-right-info-two-topic">接入方式</div>
                    <div class="card-content-right-info-two-content">
                      <span v-for="(item2, index2) in iot_access" :key="index2">
                        <span v-if="item2.value === item.access" >{{item2.label}}</span>
                      </span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <el-row>
              <el-col :span="12">
                <el-button class="card-button" icon="Edit" @click="handleUpdate(item)"
                           v-hasPermi="['iot:product:edit']">修改
                </el-button>
              </el-col>
              <el-col :span="12">
                <el-button class="card-button" icon="Delete" @click="handleDelete(item)"
                           v-hasPermi="['iot:product:remove']">删除
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-col>
      </el-row>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="productRef" :model="form" :rules="computedRules" label-width="120px">
        <el-form-item label="设备分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择设备分类">
            <el-option
                v-for="dict in categoryList"
                :key="dict.categoryId"
                :label="dict.categoryName"
                :value="dict.categoryId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="接入方式" prop="access">
          <el-select v-model="form.access" placeholder="请选择接入方式">
            <el-option
                v-for="dict in iot_access"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="摄像头用户名" prop="cameraUser" v-if="form.access == 1 || form.access == 3">
          <el-input v-model="form.cameraUser" placeholder="请输入摄像头用户名"/>
        </el-form-item>
        <el-form-item label="摄像头密码" prop="cameraPassword" v-if="form.access == 1 || form.access == 3">
          <el-input v-model="form.cameraPassword" placeholder="请输入摄像头密码"/>
        </el-form-item>
        <el-form-item label="设备ip" prop="ip" v-if="form.access == 1 || form.access == 3">
          <el-input v-model="form.ip" placeholder="请输入设备ip"/>
        </el-form-item>
        <el-form-item label="rtsp播放地址" prop="cameraRtsp" v-if="form.access == 1 || form.access == 3">
          <el-input v-model="form.cameraRtsp" placeholder="请输入rtsp播放地址"/>
        </el-form-item>
        <el-form-item label="考勤机sn码" prop="clockInSn" v-if="form.access == 0 || form.access == 2">
          <el-input v-model="form.clockInSn" placeholder="请输入考勤机sn码"/>
        </el-form-item>
        <el-form-item label="考勤机品牌" prop="clockInModel" v-if="form.access == 0 || form.access == 2">
          <el-select v-model="form.clockInModel" placeholder="请选择考勤机品牌">
            <el-option
                v-for="dict in iot_clock_in_model"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Product">
import {listProduct, getProduct, delProduct, addProduct, updateProduct} from "@/api/iot/product";
import {listVoCategory} from "@/api/iot/category";

const {proxy} = getCurrentInstance();
const {iot_clock_in_model, iot_access} = proxy.useDict('iot_clock_in_model', 'iot_access');

const productList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    categoryId: null,
    name: null,
    access: null,
  },
  rules: {
    name: [{required: true, message: '请输入设备名称', trigger: 'blur'}],
    ip: [{required: true, message: '请输入设备ip', trigger: 'blur'}],
    categoryId: [{required: true, message: '请选择分类方式', trigger: 'change'}],
    access: [{required: true, message: '请选择接入方式', trigger: 'change'}],
    cameraUser: [{required: true, message: '请输入摄像头用户名', trigger: 'blur'}],
    cameraPassword: [{required: true, message: '请输入摄像头密码', trigger: 'blur'}],
    cameraRtsp: [{required: true, message: '请输入rtsp播放地址', trigger: 'blur'}],
    clockInSn: [{required: false, message: '请输入考勤机sn码', trigger: 'blur'}],
    clockInModel: [{required: false, message: '请选择考勤机品牌', trigger: 'change'}],
  }
});

const computedRules = computed(() => {
  const rules = {...data.rules};
  if (data.form.access == 0 || data.form.access == 2) {
    rules.clockInSn[0].required = true;
    rules.clockInModel[0].required = true;
  } else {
    rules.clockInSn[0].required = false;
    rules.clockInModel[0].required = false;
  }
  return rules;
});

const {queryParams, form, rules} = toRefs(data);

/** 查询产品列表 */
function getList() {
  loading.value = true;
  listProduct(queryParams.value).then(response => {
    productList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    productId: null,
    categoryId: null,
    name: null,
    ip: null,
    access: null,
    cameraUser: null,
    cameraPassword: null,
    cameraRtsp: null,
    clockInSn: null,
    clockInModel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("productRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.productId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/**
 * 获取设备分类
 * @type {*}
 */
const categoryList = ref([]);
async function getCategory(){
  const res = await listVoCategory();
  categoryList.value = res.data;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getCategory();
  open.value = true;
  title.value = "添加产品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getCategory();
  const _productId = row.productId || ids.value
  getProduct(_productId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改产品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productRef"].validate(valid => {
    if (valid) {
      if (form.value.productId != null) {
        updateProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _productIds = row.productId || ids.value;
  proxy.$modal.confirm('是否确认删除产品编号为"' + _productIds + '"的数据项？').then(function () {
    return delProduct(_productIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('iot/product/export', {
    ...queryParams.value
  }, `product_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.card {
  width: 100%;
  padding: 20px;
}

.card-content {
  box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
  display: flex;
  padding: 20px;
  width: 100%;
  border-top: 1px solid var(--el-border-color);
  border-left: 1px solid var(--el-border-color);
  border-right: 1px solid var(--el-border-color);
}

.productImg {
  width: 80px;
  height: 80px;
}

.card-content-right {
  width: 100%;
}

.card-content-right-title {
  font-weight: 600;
  font-size: 16px;
  margin-left: 20px;
  margin-top: 10px;
}

.card-content-right-info {
  margin-left: 20px;
  margin-top: 10px;
  display: flex;
}

.card-content-right-info-two {
  margin-left: 40px;
}

.card-content-right-info-one-topic {
  color: #000000b3;
  font-size: 12px;
}

.card-content-right-info-two-topic {
  color: #000000b3;
  font-size: 12px;
}

.card-button {
  border-radius: 0 !important;
  width: 100% !important;
}
</style>